/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：GoodsGroupContorller.java
 * History:
 *       May 13, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.contorller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.entities.BaseGoodsGroup;
import com.xteam.ecsystem.service.admin.GoodsGroupService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 商品分类控制枢纽站
 * @author liuzhi
 *
 */
@Controller
public class GoodsGroupContorller {

	@Autowired
	private GoodsGroupService goodsGroupService;
	
	@Autowired
	private SystemLogService logService;
	
	public void setToBeJsonType(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/json;charset=UTF-8");
	};
	
	@RequestMapping("/getGoodsGroupList.do")
	public void getGoodsGroupList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String bggName = ReqHelper.getRequestString(request, "bggName");
		
		String strList = this.goodsGroupService.getGoodsGroupList(pageIndex, pageSize, bggName);
		response.getWriter().write(strList);
	}
	
	@RequestMapping("/saveOrUpdateGoodsGroup.do")
	public void saveOrUpdateObject(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String jsonStr = ReqHelper.getRequestString(request, "goodsGroupInfo");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD;
		
		JSONObject obj = JSONObject.fromObject(jsonStr);
		BaseGoodsGroup goodsGroup = (BaseGoodsGroup)JSONObject.toBean(obj, BaseGoodsGroup.class);
		
		if(StringUtil.isNotNullOrEmpty(goodsGroup.getBggId())){
			lslType = ConstantLocation.LOG_MODIFY;
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSGROUP_MGR, 
				"saveOrUpdateObject", lslType, "BaseGoodsGroup");
		
		String bggId = this.goodsGroupService.saveOrUpdateObject(goodsGroup, goodsGroup.getBggId());
		response.getWriter().write(bggId);
	}
	
	@RequestMapping("/getGoodsGroupInfoById.do")
	public void getGoodsGroupInfoById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bggId = ReqHelper.getRequestString(request, "bggId");		
		String strInfo = this.goodsGroupService.getGoodsGroupInfoById(bggId);
		
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/delBggById.do")
	public void delBggById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bggId = ReqHelper.getRequestString(request, "bggId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.goodsGroupService.delObject(bggId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSGROUP_MGR, 
				"delObject", ConstantLocation.LOG_DELETE_RES, "1-BaseGroupAttribute，2-BaseGoodsGroup");
	}
	
	@RequestMapping("/getBgiByBgg.do")
	public void getBgiByBgg(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bggId = ReqHelper.getRequestString(request, "bggId");

		boolean isUsed = this.goodsGroupService.getBgiByBgg(bggId);
		if (isUsed) {
			response.getWriter().write("1");
		}else{
			response.getWriter().write("0");
		}
	}
	
	@RequestMapping("/updateBggStateById.do")
	public void updateBggStateById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bggId = ReqHelper.getRequestString(request, "bggId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.goodsGroupService.updateBggStateById(bggId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSGROUP_MGR, 
				"updateBggStateById", ConstantLocation.LOG_Edit_STATE, "BaseGoodsGroup");

	}
	
	@RequestMapping("/updateBggBestById.do")
	public void updateBggBestById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bggId = ReqHelper.getRequestString(request, "bggId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.goodsGroupService.updateBggBestById(bggId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSGROUP_MGR, 
				"updateBggBestById", ConstantLocation.LOG_Edit_STATE, "BaseGoodsGroup");

	}
	
	@RequestMapping("/updateBggNewById.do")
	public void updateBggNewById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bggId = ReqHelper.getRequestString(request, "bggId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.goodsGroupService.updateBggNewById(bggId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSGROUP_MGR, 
				"updateBggNewById", ConstantLocation.LOG_Edit_STATE, "BaseGoodsGroup");

	}
	
	@RequestMapping("/updateBggHotById.do")
	public void updateBggHotById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bggId = ReqHelper.getRequestString(request, "bggId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.goodsGroupService.updateBggHotById(bggId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSGROUP_MGR, 
				"updateBggHotById", ConstantLocation.LOG_Edit_STATE, "BaseGoodsGroup");

	}
	
	@RequestMapping("/updateBggBuyById.do")
	public void updateBggBuyById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bggId = ReqHelper.getRequestString(request, "bggId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.goodsGroupService.updateBggBuyById(bggId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSGROUP_MGR, 
				"updateBggBuyById", ConstantLocation.LOG_Edit_STATE, "BaseGoodsGroup");

	}
	
	@RequestMapping("/getGoodsGroupTreeGrid.do")
	public void getGoodsGroupTreeGrid(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
//		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
//		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String bggName = ReqHelper.getRequestString(request, "bggName");
		
//		String bggTreeGrid = this.goodsGroupService.getGoodsGroupTreeGrid(request,
//				pageIndex, pageSize, bggName);
		
		String bggTreeGrid = this.goodsGroupService.getGoodsGroupTreeGrid(bggName);
		response.getWriter().write(bggTreeGrid);
	}
	
	@RequestMapping("/lazyLoadGoodsGroupTree.do")
	public void lazyLoadGoodsGroupTree(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		//此处传递的id充当下一级的pid
		String bggParent = ReqHelper.getRequestString(request, "bggId");

		String bggTree = this.goodsGroupService.lazyLoadGoodsGroupTree(bggParent);
		response.getWriter().write(bggTree);

	}
	
	@RequestMapping("/getGoodsTypeListCob.do")
	public void getGoodsTypeList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);

		String bgtList = this.goodsGroupService.getGoodsTypeList();
		response.getWriter().write(bgtList);

	}
	
	@RequestMapping("/getGoodsTypeAttrCob.do")
	public void getGoodsTypeAttrTree(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		String bgtId = ReqHelper.getRequestString(request, "bgtId");

		String bgtAttrTree = this.goodsGroupService.getGoodsTypeAttrList(bgtId);
		response.getWriter().write(bgtAttrTree);

	}
	
	@RequestMapping("/lazyLoadGoodsGroupTreeGrid.do")
	public void lazyLoadGoodsGroupTreeGrid(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		//此处传递的id充当下一级的pid
		String bggParent = ReqHelper.getRequestString(request, "bggId");

		String bggTree = this.goodsGroupService.lazyLoadGoodsGroupTreeGrid(bggParent);
		response.getWriter().write(bggTree);

	}
	
	@RequestMapping("/getBgaByBgaTattris.do")
	public void getBgaByBgaTattris(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bgaTattris = ReqHelper.getRequestString(request, "bgaTattris");

		boolean isUsed = this.goodsGroupService.getBgaByBgaTattris(bgaTattris);
		if (isUsed) {
			response.getWriter().write("1");
		}else{
			response.getWriter().write("0");
		}
	}
	
}
